﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using MoMoTransferVN_WP.utils;
using com.mservice.momo.msg;

namespace MoMoTransferVN_WP.page
{
    public partial class SetupVetifyOtpPage : PhoneApplicationPage, App.IMoMoMessageCome
    {
        long cmdIndex = 0;

        public SetupVetifyOtpPage()
        {
            InitializeComponent();
            (Application.Current as MoMoTransferVN_WP.App).setCkb((App.IMoMoMessageCome)this);
        }

        void App.IMoMoMessageCome.onMoMoMessageCome(int cmdType, long cmdIndex, byte[] cmdBody)
        {
            if (cmdType == (int)MsgType.VERIFY_OTP_REPLY && cmdIndex == this.cmdIndex)
            {
                VerifyOtpReply reply = VerifyOtpReply.CreateBuilder().MergeFrom(cmdBody).Build();
                if (reply != null && reply.Result)
                {
                    RegStatus reg = reply.RegStatus;

                    LogUtils.f("IMEY KEY " + reply.ImeiKey);
                    SettingUtils.saveSetting(Assets.KEY_IMEI_KEY, reply.ImeiKey);

                    //user status
                    SettingUtils.saveSetting(Assets.KEY_USER_SETUP, reg.IsSetup);
                    SettingUtils.saveSetting(Assets.KEY_USER_NAMED, reg.IsNamed);
                    SettingUtils.saveSetting(Assets.KEY_USER_REGISTER, reg.IsReged);

                    //setup successfull
                    SettingUtils.saveSetting(Assets.KEY_USER_SETUP, true);

                    //next to login page or register page
                    if (SettingUtils.readBool(Assets.KEY_USER_REGISTER))
                    {
                        Dispatcher.BeginInvoke(() =>
                        {
                            this.NavigationService.Navigate(new Uri("/page/LoginPage.xaml", UriKind.Relative));
                        });
                    }
                    else
                    {
                        Dispatcher.BeginInvoke(() =>
                        {
                            this.NavigationService.Navigate(new Uri("/page/RegisterPinPage.xaml", UriKind.Relative));
                        });
                    }
                }
            }       
        }
        public void onDisconnectWithError(string error)
        {
            throw new NotImplementedException();
        }
        /*
        private void btnSetup_Click(object sender, RoutedEventArgs e)
        {

            string mOtp = txtOtp.Text;
            if (mOtp.Length == 0)
            {
                LogUtils.f("Vui long nhap ma otp de tiep tuc");
                return;
            }

            cmdIndex = App.getNextMsgId();

            MomoMessage msg = MoMoReq.processVetifyOtp(cmdIndex, mOtp);
            (Application.Current as MoMoTransferVN_WP.App).mMoMoTcpClient.sendMoMoMesage(msg);
        }

        private void btnTieptuc_Click(object sender, RoutedEventArgs e)
        {
            string mOtp = txtOtp.Text;
            if (mOtp.Length == 0)
            {
                LogUtils.f("Vui long nhap ma otp de tiep tuc");
                return;
            }

            cmdIndex = App.getNextMsgId();

            MomoMessage msg = MoMoReq.processVetifyOtp(cmdIndex, mOtp);
            (Application.Current as MoMoTransferVN_WP.App).mMoMoTcpClient.sendMoMoMesage(msg);
        }
        */
        private void txtOtp_TextChanged(object sender, TextChangedEventArgs e)
        {
            if(txtOtp.Text.Length==6)
            {
                string mOtp = txtOtp.Text;
                cmdIndex = App.getNextMsgId();

                MomoMessage msg = MoMoReq.processVetifyOtp(cmdIndex, mOtp);
                (Application.Current as MoMoTransferVN_WP.App).mMoMoTcpClient.sendMoMoMesage(msg); 

            }
        }

        private void btnGuilai_Click(object sender, RoutedEventArgs e)
        {
            long cmdIndex = App.getNextMsgId();
            MomoMessage checkPhone = MoMoReq.processCheckPhone_Exist(cmdIndex, Assets.KEY_PHONE_NUMBER);
            (Application.Current as MoMoTransferVN_WP.App).mMoMoTcpClient.sendMoMoMesage(checkPhone);

        }
    }
}